<?php

namespace App\Models\College;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CollegeCategoryModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;
    protected $fillable = [
        'id',
        'pid',
        'name',
        'sort',
        'isDeleted',
        'createdAt',
        'updatedAt',
        'createdByUid',
        'updatedByUid',
    ];

    protected $table = 'college_category';

    /**
     * 查询分类列表
     * 缓存方法，使用CategoryService::getCategoryList
     */
    public static function getList()
    {
        return self::query()
            ->where('pid', 0)
            ->select('id', 'name', 'sort')
            ->orderBy('sort')
            ->get()
            ->toArray();
    }

    /**
     * 根据id查询分类详情
     * @param $categoryId
     * @return Builder|Model|object|null
     */
    public static function getCategoryInfoById($categoryId)
    {
        return self::query()->where(['id' => $categoryId, 'isDeleted' => 0])->select('id', 'name', 'sort')->first();
    }

    /**
     * 新增分类
     * @param array $data
     * @return Builder|Model
     */
    public static function addCategory($data)
    {
        return self::query()->create($data);
    }

    /**修改分类
     * @param $categoryId
     * @param $data
     * @return int
     */
    public static function setCategoryInfoById($categoryId, $data): int
    {
        return self::query()->where(['id' => $categoryId])->update($data);
    }


}
